<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="title"
    :close-on-click-modal="false"
    width="600px"
    @close="close"
  >
    <div v-loading="loading">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
        <el-form-item prop="username">
          <el-tag>请使用管理员账号找回编号</el-tag>
        </el-form-item>
        <el-form-item label="账号：" prop="username">
          <el-input v-model.trim="form.username" placeholder="请输入账户" maxlength="50" />
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input v-model.trim="form.password" type="password" placeholder="请输入密码" maxlength="50" />
        </el-form-item>
        <el-form-item v-if="form.serialNo" label="编号：" prop="serialNo">
          <div> 账号关联服务商编号为：<span class="app-color-primary">{{ form.serialNo }}</span></div>
          <div>
            <el-button
              type="text"
              @click="handleCopyClipboard(`${form.serialNo}`,$event)"
            >
              复制编号到剪切板
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button size="small" round :disabled="loading" @click="hide">取 消</el-button>
      <el-button v-if="!form.serialNo" size="small" round :disabled="loading" type="primary" @click="submitForm">找 回
      </el-button>
      <el-button v-else size="small" round type="primary" @click="handleLogin">直接登录</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { findSerial } from '@/api/user/auth'
import clip from '@/utils/clipboard'

export default {
  data() {
    return {
      // 校验字段相关
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: ['blur', 'change'] }],
        password: [{ required: true, message: '请输入密码', trigger: ['blur', 'change'] }]
      },
      title: '找回编号',
      resolve: null,
      reject: null,
      // 状态
      dialogVisible: false,
      loading: false,
      // 数据
      form: {
        username: null,
        password: null,
        serialNo: null
      }
    }
  },
  computed: {},
  watch: {},
  beforeCreate() {
  },
  created() {
  },
  beforeMount() {
  },
  mounted() {

  },
  beforeUpdate() {
  },
  update() {
  },
  activated() {
  },
  deactivated() {
  },
  beforeDestroy() {
  },
  destroyed() {
  },
  methods: {
    // 弹框相关
    open() {
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    close() {
      this.form = {
        username: null,
        password: null,
        serialNo: null
      }
      this.$refs.form.resetFields()
      this.loading = false
    },

    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const params = Object.assign({}, this.form)
          findSerial({ data: params })
            .then(res => {
              this.form.serialNo = res.data
            }).finally(() => {
              this.loading = false
            })
        }
      })
    },

    /**
     * 数据组
     */
    /**
     * 按钮组
     */
    // 复制剪切板
    handleCopyClipboard(text, event) {
      clip(text, event)
    },

    // 登录
    handleLogin() {
      this.resolve && this.resolve(JSON.stringify(this.form))
      this.hide()
    }
    /**
     * 状态组
     */
    /**
     * 方法组
     */
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
